package com.zx.jhpt.login;

import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zx.framework.plat.login.LoginAdapterController;
import com.zx.framework.plat.user.po.User;
import com.zx.framework.web.exception.AjaxException;
import com.zx.framework.web.exception.UIException;
import com.zx.jhpt.service.DeptTempService;

@Controller
public class LoginController extends LoginAdapterController {

	private static final Logger LOGGER = Logger.getLogger(LoginController.class);
	
	@Resource
	private DeptTempService deptTempService;
	
	
	/**
	 * 登录
	 * @return
	 */
	@RequestMapping("/login")
	@ResponseBody
	public Map<String, Object> login(String loginId,String password,String kaptcha){
		try {
			checkUser(loginId, password, kaptcha);
		} catch (Exception e) {
			LOGGER.error("登录失败：", e);
			throw new AjaxException(e.getMessage());
		}
		return clientMap().success().message("登录成功！").getMap();
	}
	
	/**
	 * 退出登录
	 * @return
	 */
	@RequestMapping("/loginOut")
	@ResponseBody
	public Map<String, Object> loginOut(){
		sessionInvalidate();
		return clientMap().success().message("登出成功！").getMap();
	}
	/**
	 * 单点登录
	 * @param key
	 * @return
	 */
	@RequestMapping("/sso/login/{key}")
	public String ssoLogin(@PathVariable("key") String key){
		try {
			checkSso(key);
			return "login-redirect";
		} catch (Exception e) {
			LOGGER.error("登录失败：", e);
			throw new UIException(e);
		}
	}
	/**
	 * 单点登录，获取单点登录的url
	 * @param loginId 登录id
	 * @param pwd 登录密码
	 * @param key 登录的key
	 * @return
	 */
	@RequestMapping("/sso/url")
	@ResponseBody
	protected String ssoUrl(String loginId,String pwd){
		 return getSsoUrl(loginId, pwd);
	}
}
